#include "Resource.h"

ResourceMng* ResourceMng::_instance = NULL;

/**--------------------------------------------------------
*Description : Constructor
*Method name : ResourceMng
*Parameters :
*Return value :
--------------------------------------------------------**/
ResourceMng::ResourceMng(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 backBuffer)
{
	this->d3ddv = d3ddv;
	this->backBuffer = backBuffer;

	//Load surface from file
	LoadSurfaceFile("..\\Resources\\Images\\resources_surface.txt");
	LoadSurfaceToList();

	//SURFACE, load hinh nen vao list vector

	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, L"..\\Resources\\Images\\background.jpg"));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, map_surface[1]));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, L"..\\Resources\\Images\\menu.jpg"));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, map_surface[2]));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, L"..\\Resources\\Images\\about.jpg"));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, map_surface[3]));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, L"..\\Resources\\Images\\gameover.jpg"));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, map_surface[4]));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, L"..\\Resources\\Images\\gamewin.jpg"));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, map_surface[5]));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, L"..\\Resources\\Images\\pause.jpg"));
	//surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, map_surface[6]));


	LoadTextureFile(d3ddv, "..\\Resources\\Images\\resources_texture.txt");
	LoadTextureToList();

	//TEXTURE, load hinh nhan vat vao list vector
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\land.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));

	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\mario_big.png", 5, 2, 10, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\mario_bullet.png", 5, 2, 10, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\mario_small.png", 6, 2, 12, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\mario_small_light.png", 6, 2, 12, D3DCOLOR_XRGB(253, 253, 253)));

	//CastleVania
	/*texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\player.png", 8, 1, 8, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\sprite_player_rod.png", 8, 1, 8, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\sprite_rod.png", 3, 1, 3, D3DCOLOR_XRGB(253, 253, 253)));*/



	/*texture_list.push_back(map_texture[1]);
	texture_list.push_back(map_texture[2]);
	texture_list.push_back(map_texture[3]);*/

	/*texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\candle.png", 2, 1, 2, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\rock.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\ghost.png", 4, 1, 4, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\light.png", 4, 1, 4, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\heart.png", 2, 1, 2, D3DCOLOR_XRGB(253, 253, 253)));*/

	//texture_list.push_back(map_texture[4]);
	//texture_list.push_back(map_texture[5]);
	//texture_list.push_back(map_texture[6]);
	//texture_list.push_back(map_texture[7]);
	//texture_list.push_back(map_texture[8]);

	//
	//texture_list.push_back(map_texture[9]);		//map-1-1-tiles.png
	//texture_list.push_back(map_texture[10]);	//batboss.png
	//texture_list.push_back(map_texture[11]);	//stair.png
	//texture_list.push_back(map_texture[12]);	//map-1-2-tiles.png
	//texture_list.push_back(map_texture[13]);	//sprite_stair.png	
	//texture_list.push_back(map_texture[14]);	//sprite_stair.png	
	//texture_list.push_back(map_texture[15]);	//hit_die.png	
	//texture_list.push_back(map_texture[16]);	//dog_enemy.png	
	//texture_list.push_back(map_texture[17]);	//red_bat.png	
	//texture_list.push_back(map_texture[18]);	//red_dragon.png
	//texture_list.push_back(map_texture[19]);	//map-1-3-tiles.png
	//texture_list.push_back(map_texture[20]);	//all_item.png
	//texture_list.push_back(map_texture[21]);	//pot.png
	//texture_list.push_back(map_texture[22]);	//map-1-4-tiles.png
	//texture_list.push_back(map_texture[23]);	//map-1-5-tiles.png
	//texture_list.push_back(map_texture[24]);	//map-1-6-tiles.png

	//texture_list.push_back(map_texture[25]);	//info_heart.png
	//texture_list.push_back(map_texture[26]);	//info_score.png
	//texture_list.push_back(map_texture[27]);	//info_time.png
	//texture_list.push_back(map_texture[28]);	//info_life.png
	//texture_list.push_back(map_texture[29]);	//info_world.png
	//texture_list.push_back(map_texture[30]);	//star.png
	//texture_list.push_back(map_texture[31]);	//score.png
	//texture_list.push_back(map_texture[32]);	//sprite_rod_v2.png
	//texture_list.push_back(map_texture[33]);	//sprite_rod_v3.png
	//texture_list.push_back(map_texture[34]);	//info_mana.png
	//texture_list.push_back(map_texture[35]);	//info_death.png
	//texture_list.push_back(map_texture[36]);	//info_blood_boss.png
	//texture_list.push_back(map_texture[37]);	//info_boss.png
	//texture_list.push_back(map_texture[38]);	//info_player.png
	//texture_list.push_back(map_texture[39]);	//info_item.png
	//texture_list.push_back(map_texture[40]);	//dagger.png
	//texture_list.push_back(map_texture[41]);	//holy_water.png
	//texture_list.push_back(map_texture[42]);	//holy_water_light.png

	//////////////////////////////////////////////////////////////////////////

	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\stone.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));

	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\brick_32.png", 2, 3, 6, D3DCOLOR_XRGB(253, 253, 253)));

	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\exp_brick.png", 2, 1, 2, D3DCOLOR_XRGB(253, 253, 253)));

	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\pipe_min.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\pipe_mid.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\pipe_max.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\pipe_ngang.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));

	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\cloud.png", 2, 1, 2, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\grass.png", 2, 1, 2, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\mountain.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\item_32.png", 4, 2, 8, D3DCOLOR_XRGB(253, 253, 253)));

	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\pirhana_32.png", 2, 1, 2, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\goomba_32.png", 11, 2, 22, D3DCOLOR_XRGB(253, 253, 253)));
	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\turtle.png", 6, 2, 12, D3DCOLOR_XRGB(253, 253, 253)));



	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\score.png", 10, 1, 10, D3DCOLOR_XRGB(253, 253, 253)));

	//texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\star.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	/*texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\infomario.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\infocoin.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\infoworld.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\inforesult.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\infotime.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));*/

	/*texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\checkpoint.png", 1, 1, 1, D3DCOLOR_XRGB(253, 253, 253)));
	texture_list.push_back(new Texture(this->d3ddv, L"..\\Resources\\Images\\bullet.png", 4, 2, 8, D3DCOLOR_XRGB(253, 253, 253)));*/


}


/**--------------------------------------------------------
*Description : Destructor
*Method name : ~ResourceMng
*Parameters :
*Return value :
--------------------------------------------------------**/
ResourceMng::~ResourceMng(void)
{
	surface_list.clear();
	texture_list.clear();
}


/**--------------------------------------------------------
*Description : Lay texture
*Method name : GetTexture
*Parameters :   LPWSTR fileName
*Return value : *TEXTURE
--------------------------------------------------------**/
Texture* ResourceMng::GetTexture(LPWSTR fileName)
{
	int size = texture_list.size();
	Texture* p_texture;

	for (int i = 0; i < size; ++i){
		p_texture = texture_list[i];

		if (p_texture->getFileName() == fileName){
			return p_texture;
		}
	}

	//GLMessage(L"Can not GetTexture");
	return NULL;
}


/**--------------------------------------------------------
*Description : Lay surface
*Method name : GetSurface
*Parameters :   LPWSTR fileName
*Return value : *SURFACE
--------------------------------------------------------**/
Surface* ResourceMng::GetSurface(LPWSTR fileName){
	int size = surface_list.size();
	Surface* p_surface;

	for (int i = 0; i < size; ++i){
		p_surface = surface_list[i];

		if (p_surface->getFileName() == fileName){
			return p_surface;
		}
	}

	//GLMessage(L"Can not GetSurface");
	return NULL;
}


/**--------------------------------------------------------
*Description : Lay doi tuong Resource
*Method name : GetInst
*Parameters :
*Return value : *ResourceMng
--------------------------------------------------------**/
ResourceMng* ResourceMng::GetInst(LPDIRECT3DDEVICE9 d3ddv, LPDIRECT3DSURFACE9 backBuffer){
	if (_instance == NULL){
		_instance = new ResourceMng(d3ddv, backBuffer);
	}

	return _instance;
}

/*
*	Load surface from file
*/
void ResourceMng::LoadSurfaceFile(char* file_name)
{
	string data;
	ifstream fileStream(file_name);
	if (fileStream.is_open())
	{
		while (!fileStream.eof())
		{
			data += fileStream.get();
		}
	}
	vector<string> dataSplit = Utility::split(data, '\n');
	string row_header = dataSplit[0];
	vector<string> element_row = Utility::split(row_header, ' ');

	for (int i = 1; i < dataSplit.size(); i++)
	{
		string temp = dataSplit[i];
		vector<string> temp_vector = Utility::split(temp, ' ');

		int id_key = atoi(temp_vector[0].c_str());
		string path_file = temp_vector[1].c_str();

		map_surface[id_key] = Utility::convertStringToLPWSTR(path_file);
	}

}

/*
*	Load texture from file
*/
void ResourceMng::LoadTextureFile(LPDIRECT3DDEVICE9 d3ddv, char* file_name)
{
	string data;
	ifstream fileStream(file_name);
	if (fileStream.is_open())
	{
		while (!fileStream.eof())
		{
			data += fileStream.get();
		}
	}
	vector<string> dataSplit = Utility::split(data, '\n');
	string row_header = dataSplit[0];
	vector<string> element_row = Utility::split(row_header, ' ');

	for (int i = 1; i < dataSplit.size(); i++)
	{
		string temp = dataSplit[i];
		vector<string> temp_vector = Utility::split(temp, ' ');

		int id_key = atoi(temp_vector[0].c_str());
		string path_file = temp_vector[1].c_str();
		int cols = atoi(temp_vector[2].c_str());
		int rows = atoi(temp_vector[3].c_str());
		int count = atoi(temp_vector[4].c_str());

		Texture *p_texture = new Texture(d3ddv, Utility::convertStringToLPWSTR(path_file), cols, rows, count, D3DCOLOR_XRGB(253, 253, 253));

		map_texture[id_key] = p_texture;

	}

}

void ResourceMng::LoadTextureToList()
{
	map<int, Texture*>::iterator i;
	for (i = map_texture.begin(); i != map_texture.end(); i++)
	{
		texture_list.push_back((*i).second);
	}
}

void ResourceMng::LoadSurfaceToList()
{
	map<int, LPWSTR>::iterator i;
	for (i = map_surface.begin(); i != map_surface.end(); i++)
	{
		surface_list.push_back(new Surface(this->d3ddv, this->backBuffer, (*i).second));
	}
}
